﻿using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using Ionic.Zip;
using Ro.AssistFunction.OutputManage;
using Ro.Common.RoArgs;
using Ro.Common.RoInterface;
using Ro.Common.RoInterface.UserType;

namespace Ro.Interpreter.InterAssistFunc
{
    /// <summary>
    /// 基于给定的App信息，进行解压文件
    /// </summary>
    public class UnZipDriver
    {
        /// <summary>
        /// 解压文件
        /// </summary>
        /// <param name="appInfo">appinfo信息</param>
        /// <returns>返回完成是否正确</returns>
        public static bool ExtractZipFileIntoFolder(AppInfo appInfo)
        {
            try
            {
                string appname = appInfo.AppName;
                string bits = appInfo.BaseWindowsBits;


                //todo 5-15更新Gui版本
                string str;
                if (!RoPubArgments.ProTips)
                {
                    str = (from var in RoPubArgments.SystemProperties where var.Id == "ProjectPath" select var).Single().Value;
                }
                else
                {
                    str = Environment.CurrentDirectory;
                }

             


                //压缩包路径
                string zipString = str + "/Package/" + appname + "_" + bits + ".zip";
                if (!File.Exists(zipString))
                {
                    //添加输出
                    if (RoPubArgments.ProTips)
                    {
                        RoOutputToGui roOutputToGui = new RoOutputToGui();
                        //获取当前时间
                        string nowtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        roOutputToGui.OutDetail(nowtime, "方法ExtractZipFileIntoFolder执行失败，路径" + zipString + "不存在");
                    }
                    else
                    {
                        RoPubArgments.RoOutputToConsole.OutputConsole(RoLogStatus.Fatal, "方法ExtractZipFileIntoFolder执行失败，路径" + zipString + "不存在");
                    }
                 
                    RoPubArgments.RoLog.Fatal("方法ExtractZipFileIntoFolder执行失败，路径" + zipString + "不存在");
                    return false;
                }
                //设置路径
                ZipFile zip = new ZipFile(zipString);


                //判断文件是否存在
                if (!Directory.Exists(appInfo.ExecuePath))
                {
                    //解压
                    zip.ExtractAll(appInfo.ExecuePath);
                    Thread.Sleep(1500);

                    //写出路径
                    if (appInfo.AppName == "Chrome")
                    {
                        RoPubArgments.BrowserDriverPath = appInfo.ExecuePath + "/";
                    }
                    else
                    {
                        RoPubArgments.BrowserDriverPath = appInfo.ExecuePath + "/" + appname + "_" + bits + ".zip";
                    }
                }
                else
                {
                    //写出路径
                    if (appInfo.AppName == "Chrome")
                    {
                        RoPubArgments.BrowserDriverPath = appInfo.ExecuePath + "/";
                    }
                    else
                    {
                        RoPubArgments.BrowserDriverPath = appInfo.ExecuePath + "/" + appname + "_" + bits + ".zip";
                    }
                }
                return true;
            }
            catch (Exception e)
            {
                //添加输出
                ExceptionOutPut.SystemExceptionOut("ExtractZipFileIntoFolder", e);
                return false;
            }
        }
    }
}
